	var leadCompIds = new Array(
					"companyName","street1","street2","country","province","city","web","industryType",
					"leadStatusType","firstName","middleInitial","lastName","title","email","phone1","phone2",
					"phone3","phone4","source","category","grade","leadStage","action","closingProbability","owner",
					"notes","companySize","annualRevenue","competitor1","competitor2","competitor3"
					);
		  	
	var leadValidationCompIds = new Array(
					"companyName","street1","country","province","city","industryType",
					"firstName","title","email","phone1",
					"source","category","grade","leadStage","action","closingProbability","owner",
					"companySize","annualRevenue"
					);

	var staticLeadId = '0';



  function createLeadPanel(){

		var leadSearchCriteriaStore=CRM.commons.createComboStore('leadSearchCriteriaStoreid', 'lead/leadSearchCriteriaTypes');
    	
    	var leadStore=CRM.commons.createStore('leadStoreid',CRM.SPComp.leadDataArray,'searchleads/showLeads');
    	
    	var activityStore=CRM.commons.createStore('activityStoreid',CRM.SPComp.activityDataArray,'searchactivities/showActivitiesByLead');
    	
    	var contactStore=CRM.commons.createStore('contactStoreid',CRM.SPComp.contactDataArray,'searchleads/showContacts');    	
    	
    	var opportunityStore = CRM.commons.createStore('opportunityStoreid',CRM.SPComp.opportunityDataArray,'searchopportunities/showOpportunitiesByLead');
    	
        var leadStatusStore=CRM.commons.createComboStore('leadStatusStoreid','lead/leadStatusTypes','');

        var industryStore=CRM.commons.createComboStore('industryStoreid','lead/leadReqCombo','industryTypes');
      
        var sourceStore=CRM.commons.createComboStore('sourceStoreid','lead/leadReqCombo','sourceTypes');

        var categoryStore=CRM.commons.createComboStore('categoryStoreid','lead/leadReqCombo','categoryTypes');

        var gradeStore=CRM.commons.createComboStore('gradeStoreid','lead/leadReqCombo','gradeTypes');

        var stageStore=CRM.commons.createComboStore('stageStoreid','lead/leadReqCombo','stageTypes');

        var actionStore=CRM.commons.createComboStore('actionStoreid','lead/leadReqCombo','actionTypes');

        var closingProbabilityStore=CRM.commons.createComboStore('closingProbabilityStoreid','lead/leadReqCombo','closingProbabilityTypes');

        var countryStore=CRM.commons.createComboStore('countryStoreid','lead/countries','');

        var provinceStore=CRM.commons.createComboStore('provinceStoreid','lead/provinces','');

        var ownerStore=CRM.commons.createComboStore('ownerStoreid','lead/owners','');
        
        var uploadDocStore=CRM.commons.createStore('uploadDocStoreid',CRM.SPComp.uploadDocDataArray,'documents/docList');
        
  	  //URL for dashboard search form
        
        var assignedToDashboardStore = CRM.commons.createComboStore('assignedToDashboardStoreid','searchleads/salespersonTypes');
        var gridDashboardStore = CRM.commons.createComboStore('gridDashboardStoreid','searchleads/gradeDashboardTypes','');
        var stageDashboardStore = CRM.commons.createComboStore('stageDashboardStoreid','searchleads/stageDashboardTypes','');
        var leadDashboardStore = CRM.commons.createStore('leadDashboardStoreid',CRM.SPComp.leadDataArray,'searchleads/showLeadDetails');
     

        //Phone Validation
        Ext.apply(Ext.form.VTypes, {
        		phone: function(value,field){  
        					return value.replace(/[ \-\(\)]/g,'').length == 10;  
        				},  
        	phoneText: 'Wrong phone number, please make sure it contains 10 digits',  
	        phoneMask: /[ \d\-\(\)]/  
        });

         
                var leadPanel = {
                    xtype       : 'fieldset',
                    id          : 'leadPanel',
                    title       : 'Lead Information',
                    height      : 220,
                    width       : 525,
                    labelWidth  : 100,
                    layout      : 'form',
                    items       : [
									{
									    xtype   : 'hidden',
									    id      : 'leadId',
									    value	: '0'
									},
                                    {
                                      xtype     : 'compositefield',
                                      fieldLabel: 'Name<font color=red>*</font>',
                                      items     : [
													
                                                    {
                                                        xtype   : 'textfield',
                                                        id      : 'firstName',
                                                        width   :  175
                                                        //allowBlank : false
                                                     },
                                                     {
                                                        xtype   : 'textfield',
                                                        id      : 'middleInitial',
                                                        width   :  30
                                                     },
                                                     {
                                                        xtype   : 'textfield',
                                                        id      : 'lastName',
                                                        width   :  175
                                                     }
                                                   ]
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'title',
                                        fieldLabel  : 'Title<font color=red>*</font>',
                                        width       : 180
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'email',
                                        fieldLabel  : 'Email<font color=red>*</font>',
                                        //vtype       :'email',
                                        width       : 390
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'phone1',
                                        fieldLabel  : 'Phone (Work)<font color=red>*</font>',
                                        width       : 220
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'phone2',
                                        fieldLabel  : 'Phone (Mobile)',
                                        width       : 220,
                                        vtype       :'phone'
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'phone3',
                                        fieldLabel  : 'Phone (Home)',
                                        width       : 220,
                                        vtype       :'phone'
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'phone4',
                                        fieldLabel  : 'Fax',
                                        width       : 220,
                                        vtype       :'phone'
                                    }
                                  ]
                };

                var companyPanel = {
                    xtype       : 'fieldset',
                    id          : 'companyPanel',
                    title       : 'Company Information',
                    height      : 220,
                    width       : 525,
                    labelWidth  : 100,
                    layout      : 'form',
                    items       : [
                                    {
                                        xtype       : 'textfield',
                                        id          : 'companyName',
                                        fieldLabel  : 'Company Name<font color=red>*</font>',
                                        width       : 390
                                        //allowBlank  : false,
                                        //msgTarget	: 'under'
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'street1',
                                        fieldLabel  : 'Address1<font color=red>*</font>',
                                        //allowBlank  : false,
                                        width       : 390
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'street2',
                                        fieldLabel  : 'Address2',
                                        width       : 390
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'city',
                                        fieldLabel  : 'City<font color=red>*</font>',
                                        width       : 160
                                    },
                                    {
                                        xtype       : 'compositefield',
                                        width       : 400,
                                        fieldLabel  : 'Province<font color=red>*</font>',
                                        items       : [
                                                       {
                                                           xtype    	: 'textfield',
                                                           id       	: 'province',
                                                           store		: provinceStore,
                                                           displayField : 'criteria',
                                                           valueField  	: 'id',
                                                           triggerAction: 'all',
                                                           width		: 160,
                                                           editable		: false
                                                       },
                                                       {xtype:'displayfield', value: 'Country<font color=red>*</font>:'},                                                       
                                                       {
                                                           xtype    	: 'textfield',
                                                           id       	: 'country',
                                                           fieldLabel   : 'Country',
                                                           store		: countryStore,
                                                           displayField : 'criteria',
                                                           valueField   : 'id',
                                                           triggerAction: 'all',
                                                           width		: 165,
                                                           editable	: false
                                                       }
                                                       
                                                      ]
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'web',
                                        fieldLabel  : 'Web',
                                        width       : 390
                                        //vtype       : 'url'
                                        //emptyText   : 'http://www.sourcepundit.com'
                                    },
                                    {
                                        xtype       : 'compositefield',
                                        width       : 400,
                                        fieldLabel  : 'Industry Type<font color=red>*</font>',
                                        items		: [
														{
														    xtype       : 'combo',
														    id          : 'industryType',
														    //fieldLabel  : 'Industry Type',
														    store		: industryStore,
														    displayField: 'name',
														    valueField  : 'id',
														    value       :'SELECT',
														    triggerAction : 'all',
														    width       : 160,
														    editable	: false,
														    listeners	: {select : function(){
														    						bindValue('industryType', 'industryTypeId');
														    					} 
														    			  }
														},
														{
															xtype		: 'hidden',
															id			: 'industryTypeId'
														},
														{xtype:'displayfield', value: 'Status<font color=red>*</font>:', width: 47},  
														{
														    xtype       : 'combo',
														    id          : 'leadStatusType',
														    //fieldLabel  : 'Status',
														    store		: leadStatusStore,
														    displayField: 'criteria',
														    valueField  : 'id',
														    value		: 'SELECT',
														    triggerAction : 'all',
														    width       : 170,
														    editable	: false,
														    listeners	: {select : function(){
														    						bindValue('leadStatusType', 'leadStatusTypeId');
														    					} 
														    			  }
														},
														{
															xtype		: 'hidden',
															id			: 'leadStatusTypeId',
															value		: '0'
														}
                                             		   ]
                                    }
                                  ]
                };

	                var basicInfoPanel = {
	                    xtyp        : 'panel',
	                    id          : 'basicInfoPanel',
	                    name		: 'basicInfoPanel',
	                    width       : 1085,
	                    autoHeight  : true,
	                    layout      : 'hbox',
	                    padding     : '5px',
	                    items       : [companyPanel, leadPanel],
	                    frame       : true,
	                    tbar    	: {  xtype       : 'toolbar',
				                          buttonAlign : 'right',
				                          id		  : 'leadTopToolBar',
				                          items       : [
				                                          /*
														   {
														    xtype   : 'button',
														    id		: 'topNewButton',
														    text    : 'New',
														    handler	: function(){
														    				newLeadForm();
														    				var form = Ext.getCmp('mainPanel');
														    				form.findById('leadSubTabPanel').activate(0);
														    			}
														   },
														   '-',
														   */
														   {
															xtype   : 'button',
				                                            id		: 'topEditButton',
				                                            text    : 'Edit',
				                                            handler :  function(){
				                                          				editLeadForm();
				                                          			}
														   },
														   '-',
														   {
															xtype   : 'button',
				                                            id	  	: 'topSaveButton',
				                                            text    : 'Save',
				                                            handler : function(){
				                                            			ValidateAndSaveLead();
				                                            		   }
														   },
														   '-',
														   {
					                                        xtype   : 'button',
					                                        id	  	: 'topRemoveButton',
					                                        text    : 'Cancel',
					                                        handler : function(){
					                                        			cancelLeadEntryForm();
					                                        		 }
														   }
				                                      ]
	                			}
	                };


                var leadProfilePanel = {
                    xtype       : 'fieldset',
                    id          : 'leadProfilePanel',
                    title       : 'Lead Profile',
                    layout      : 'form',
                    labelWidth  : 100,
                    width       : 525,
                    height      : 175,
                    items       : [
									{
									    xtype       : 'compositefield',
									    fieldLabel  : 'Source<font color=red>*</font>',
									    items       : [
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'source',
									                       store		: sourceStore,
					                                       displayField : 'name',
					                                       valueField	: 'id',
					                                       value        :'SELECT',
					                                       triggerAction: 'all',
									                       width	 	: 160,
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('source', 'sourceId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'sourceId'
									                    },
									                   {xtype:'displayfield', value: 'Category<font color=red>*</font>:', width : 65},
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'category',
									                       store		: categoryStore,
					                                       displayField	: 'name',
					                                       valueField  	: 'id',
					                                       value        :'SELECT',
					                                       triggerAction: 'all',
									                       width		: 160,
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('category', 'categoryId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'categoryId'
									                    }
									                  ]
									},
									{
									    xtype       : 'compositefield',
									    fieldLabel  : 'Grade<font color=red>*</font>',
									    items       : [
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'grade',
									                       store		: gradeStore,
					                                       displayField : 'name',
					                                       valueField  	: 'id',
					                                       value        :'SELECT',
					                                       triggerAction: 'all',
									                       width		: 160,
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('grade', 'gradeId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'gradeId'
									                    },
									                   {xtype:'displayfield', value: 'Stage<font color=red>*</font>:', width : 65},
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'leadStage',
									                       store		: stageStore,
					                                       displayField : 'name',
					                                       valueField  	: 'id',
					                                       triggerAction: 'all',
									                       width		: 160,
									                       value		: 'SELECT',
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('leadStage', 'leadStageId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'leadStageId'
									                    }
									                  ]
									},
									{
									    xtype       : 'compositefield',
									    fieldLabel  : 'Action<font color=red>*</font>',
									    items       : [
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'action',
									                       store		: actionStore,
					                                       displayField : 'name',
					                                       valueField  	: 'id',
					                                       value        :'SELECT',
					                                       triggerAction: 'all',
									                       width		: 160,
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('action', 'actionId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'actionId'
									                    },
									                   {xtype:'displayfield', value: 'Prob.<font color=red>*</font>:', width : 65},
									                   {
									                       xtype    	: 'combo',
									                       id       	: 'closingProbability',
									                       store		: closingProbabilityStore,
					                                       displayField : 'name',
					                                       valueField  	: 'id',
					                                       value        :'SELECT',
					                                       triggerAction: 'all',
									                       width		: 160,
									                       editable		: false,
									                       listeners	: {select : function(){
									                        						bindValue('closingProbability', 'closingProbabilityId');
									                        					} 
									                        			  }
									                    },
									                    {
									                    	xtype		: 'hidden',
									                    	id			: 'closingProbabilityId'
									                    }
									                  ]
									},
									{
                                        xtype       : 'compositefield',
                                        width       : 400,
                                        fieldLabel  : 'Owner<font color=red>*</font>',
                                        items       : [
														{
														    xtype       : 'combo',
														    id          : 'owner',
														    //fieldLabel  : 'Owner',
														    store		: ownerStore,
														    displayField: 'name',
														    valueField  : 'id',
														    value       :'SELECT',
														    triggerAction : 'all',
														    width       : 160,
														    editable	: false,
														    listeners	: {select : function(){
														    						bindValue('owner', 'ownerId');
														    					} 
														    			  }
														},
														{
															xtype		: 'hidden',
															id			: 'ownerId'
														}
                                                       ]
									}
                                    ,
                                    {
                                        xtype       : 'textarea',
                                        id          : 'notes',
                                        fieldLabel  : 'Notes',
                                        width       : 390,
                                        height      : 35
                                    }
                                  ]
                };

                var companyProfilePanel = {
                    xtype       : 'fieldset',
                    id          : 'companyProfilePanel',
                    title       : 'Company Profile',
                    layout      : 'form',
                    labelWidth  : 100,
                    width       : 525,
                    height      : 175,
                    items       : [
                                    {
                                        xtype       : 'textfield',
                                        id          : 'companySize',
                                        fieldLabel  : 'Company Size',
                                        value		: '0',
                                        width       : 120
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'annualRevenue',
                                        fieldLabel  : 'Annual Revenue',
                                        value		: '0',
                                        width       : 120
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'competitor1',
                                        fieldLabel  : 'Competitor',
                                        width       : 390
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'competitor2',
                                        fieldLabel  : 'Competitor',
                                        width       : 390
                                    },
                                    {
                                        xtype       : 'textfield',
                                        id          : 'competitor3',
                                        fieldLabel  : 'Competitor',
                                        width       : 390
                                    }
                                  ]
                };
                


		                var leadSubTabPanel = {
		                    xtype       : 'tabpanel',
		                    id          : 'leadSubTabPanel',
		                    width       : 1085,
		                    height      : 250,
		                    activeTab   : 0,
		                    frame       : true,
		                    items       : [
		                                    {
		                                        xtype   : 'panel',
		                                        title   : 'Lead Analysis',
		                                        id	    : 'leadAnalysisTab',
		                                        frame   : true,
		                                        layout  : 'hbox',
		                                        padding : '5px',
		                                        items   : [leadProfilePanel, companyProfilePanel],
		            		                    bbar        : {
							                                    xtype       : 'toolbar',
							                                    buttonAlign : 'right',
							                                    id			: 'leadBottomToolBar',
							                                    items       : [
							                                                   /*
																			   {
																			    xtype   : 'button',
																			    id		: 'newButton',
																			    text    : 'New',
																			    handler	: newLeadForm
																			   },
																			   '-',
																			   */
							                                                   {
							                                                    xtype   : 'button',
							                                                    id		: 'editButton',
							                                                    text    : 'Edit',
							                                                    handler :  function(){
							                                                    				editLeadForm();
							                                                    			}
							                                                   },
																			   '-',
							                                                   {
							                                                    xtype   : 'button',
							                                                    id		: 'saveButton',
							                                                    text    : 'Save',
							                                                    handler : function(){
							                                                    			 ValidateAndSaveLead();
							                                                    		   }
							                                                   },
																			   '-',
							                                                   {
							                                                    xtype   : 'button',
							                                                    id		: 'removeButton',
							                                                    text    : 'Cancel',
							                                                    handler : function(){
								                                        			cancelLeadEntryForm();
								                                        		 }
							                                                   }
							                                                ]
							                                  }
		                                    },
		                                    
		                                    {
		                                    	xtype 	: 'panel',
		                                    	id		: 'opportunitiesPanel',
		                                    	title 	: 'Opportunities',
		                                    	items 	: [CRM.commons.createGrid('opportunitiesGrid', opportunityStore, CRM.SPComp.createOpportunityColumnModel())],
		                                    	tbar    : {
				                                    xtype       : 'toolbar',
				                                    buttonAlign : 'left',
				                                    items       : [
																   '-',
				                                                   {
				                                                    xtype   : 'button',
				                                                    id		: 'addNewOpportunityButton',
				                                                    text    : 'Add New',
				                                                    handler :  function(){
				                                                    			 var form = Ext.getCmp('mainPanel').getForm();
					                                                             var selectedLeadId = form.findField('leadId').getValue();
			    																 CRM.SPComp.showOpportunityWindow(selectedLeadId,'0',null);
				                                                     			}
				                                                   },
																   '-'
				                                                ]
				                                  }
		                                    },
		                                    
		                                    {
		                                    	xtype : 'panel',
		                                    	id	  : 'activitiesPanel',
		                                    	title : 'Activities',
		                                    	activeTab : 0,
		                                    	items : [CRM.commons.createGrid('activitiesGrid', activityStore, CRM.SPComp.createActivityColumnModel())],
		                                    	tbar        : {
				                                    xtype       : 'toolbar',
				                                    buttonAlign       : 'left',
				                                    items       : [
																   '-',
				                                                   {
				                                                    xtype   : 'button',
				                                                    id		: 'addNewActivityButton',
				                                                    text    : 'Add New',
				                                                    handler :  function(){
				                                                    				var form = Ext.getCmp('mainPanel').getForm();
         		    																var selectedLeadId = form.findField('leadId').getValue();
				                                                    				showLeadActivityScreen(true, selectedLeadId, '0');
				                                                    			}
				                                                   },
																   '-'
				                                                ]
				                                  }
		                                    },
		                                    {
		                                    	xtype : 'panel',
		                                    	id	  : 'additionalContactPanel',
		                                    	title : 'Additional Contact',
		                                    	activeTab : 0,
		                                    	items : [CRM.commons.createGrid('contactsGrid', contactStore, CRM.SPComp.createContactColumnModel())],
		                                    	tbar        : {
				                                    xtype       : 'toolbar',
				                                    buttonAlign       : 'left',
				                                    items       : [
																   '-',
				                                                   {
				                                                    xtype   : 'button',
				                                                    id		: 'addNewContactButton',
				                                                    text    : 'Add New',
				                                                    handler :  function(){
				                                                    				var form = Ext.getCmp('mainPanel').getForm();
         		    																var selectedLeadId = form.findField('leadId').getValue();
				                                                    				showContactScreen(true, selectedLeadId, '0');
				                                                    			}
				                                                   },
																   '-'
				                                                ]
				                                  }
		                                    },
		                                    {
		                                    	xtype : 'panel',
		                                    	id	  : 'documentPanel',
		                                    	title : 'Document',
		                                    	activeTab : 0,
		                                    	items : [CRM.commons.createGrid('leadDocGrid', uploadDocStore, CRM.SPComp.createUploadDocColumnModel())],
		                                    	tbar        : {
				                                    xtype       : 'toolbar',
				                                    buttonAlign       : 'left',
				                                    items       : [
																   '-',
				                                                   {
																	icon	: '../images/upload.jpg',
				                                                    id		: 'uploadNewDocumentButton',
				                                                    tooltip : 'Upload New',
				                                                    handler :  function(){
				                                                                	var formPanel = Ext.getCmp('mainPanel');
				                                                                	var leadId = formPanel.getForm().findField('leadId').getValue();
				                                                    				showDocumentUploader(leadId);
				                                                    			}
				                                                   },
																   '-',
																   {
				                                                	    icon	: '../images/refresh.jpg',
					                                                    id		: 'showDocumentButton',
					                                                    //text    : 'Show',
					                                                    tooltip	: 'Refresh',
					                                                    handler :  function(){
					                                                    				var form = Ext.getCmp('mainPanel');
					                                                    				var myUploadGrid = form.findById('leadDocGrid');
					                                                    				var selectedLeadId = form.getForm().findField('leadId').getValue();
					                                                    				showDoc(selectedLeadId, myUploadGrid);//this method is in customer.js
					                                                    			}
					                                                   },
																	'-'
				                                                ]
				                                  }
		                                    }
		                                    
		                                  ]
		                };
		                

                var searchTab = new Ext.Panel({
                	xtype 	: 'panel',
		     	    id		: 'searchTab',
		     	    title	: 'Search',
		     	    width	: 1100,
		     	    items	: [CRM.SPComp.createSearchComp('spSearchLeadGridPanel', 'searchCriteria', 'criteriaValue', leadStore, CRM.SPComp.createLeadColumnModel('spSearchLeadGridPanel'), leadSearchCriteriaStore)]
                });
                
                var combineDetailsPanel = new Ext.Panel({
                	xtype	: 'panel',
                	id		: 'combineDetailsPanel',
                	width	: 1100,
                	autoHeight: true,
                	items	: [basicInfoPanel,leadSubTabPanel]
                });
                
                var detailsTab = new Ext.Panel({
                	xtype	: 'panel',
		     	    id		: 'detailsTab',
		     	    title	: 'Details',
		     	    width	: 1100,
		     	    autoHeight : true,
		     	    disabled : true,
		     	    items	: [combineDetailsPanel],
	     	    	listeners: {activate: function(){
	     	    					if(staticLeadId != '0'){
	     	    						showLeadDetails(staticLeadId);
	     	    					}
	     	    					switchForm(combineDetailsPanel, detailsTab);
	     	    				 }
	     	    				}
                });
                
                var dashBoardTab = new Ext.Panel({
	     	    	xtype	: 'panel',
	     	    	id		: 'dashBoardTab',
	     	    	title	: 'Dash Board',
	     	    	width	: 1080,
	     	    	autoHeight : true,
	     	    	items	:  [CRM.SPComp.createLeadDashboardSearchComp('leadDashBoard',leadDashboardStore, CRM.SPComp.createLeadColumnModel('leadDashBoard'),assignedToDashboardStore,gridDashboardStore,stageDashboardStore)]
               });
                
               var newLeadTab = new Ext.Panel({
	     	    	xtype	: 'panel',
	     	    	id		: 'newLeadTab',
	     	    	title	: 'New',
	     	    	width	: 1080,
	     	    	autoHeight : true,
	     	    	items	:  [],
	     	    	listeners: {activate: function(){
	     	    					switchForm(combineDetailsPanel, newLeadTab);
	     	    					newLeadForm();
	     	    				 }
	     	    				}
               });
                
                var leadMainTab = new Ext.TabPanel({
                		xtype	: 'tabpanel',
                		id		: 'leadMainTab',
                		activeTab : 0,
                		anchor	: 1100,
                		height	: 650,
                		items	: [searchTab, dashBoardTab, detailsTab, newLeadTab]
                });
                
      
                var mainPanel = new Ext.FormPanel({
                    id          : 'mainPanel',
                    height      : 710,
                    anchor      : 1100,
                    frame       : false,
                    layout      : 'vbox',
                    items       : [
                                    leadMainTab,
                                    
                                  ]       
                });
                var form = Ext.getCmp('mainPanel');
                form.findById('activitiesPanel').setAutoScroll(true);
                form.findById('opportunitiesPanel').setAutoScroll(true);
                form.findById('additionalContactPanel').setAutoScroll(true);
                form.findById('documentPanel').setAutoScroll(true);
                validateFormOnLoad(mainPanel, leadValidationCompIds);
      
         return mainPanel;
  	};

  	
             function ValidateAndSaveLead(){
	             var form = Ext.getCmp('mainPanel');
	 			 var validationSuccess = simpleFormValidation(form, leadValidationCompIds);
	 			 	if(validationSuccess == 'true'){
	 					saveLead();
	 				}
           	 };
               
            //onload calls
 			function showLeadDetails(selectedLeadId){
				    var formPanel = Ext.getCmp('mainPanel');
				    staticLeadId = selectedLeadId; 
				    formPanel.getForm().load({
					method  : 'GET',
 		            url     : 'lead/showLeadDetails?leadId='+selectedLeadId,
 		      		success : function() {
 		      					disableLeadForm();
 		      					formPanel.findById('detailsTab').enable();
 		      					
 		      					var lmt					= formPanel.findById('leadMainTab');
 		      					var activityGrd			= lmt.findById('activitiesGrid');
 		      					var contactsGrd			= lmt.findById('contactsGrid');
 		      					var opportunitiesGrd	= lmt.findById('opportunitiesGrid');
 		      					var leadDocGrd			= lmt.findById('leadDocGrid');
 		      					
 		      					reloadLeadSubTabs(activityGrd, selectedLeadId);
 		      					reloadLeadSubTabs(contactsGrd, selectedLeadId);
 		      					reloadLeadSubTabs(opportunitiesGrd, selectedLeadId);
 		      					reloadLeadSubTabs(leadDocGrd, selectedLeadId);
 		            		 },
 		            failure :function() { 
 		                		alert ("Failure");
 		               		 }
					});
   				    formPanel.findById('leadMainTab').setActiveTab('detailsTab');
				};
				

				function showLeadDashBoardDetails(selectedLeadId){
				    var formPanel = Ext.getCmp('mainPanel');	
				    formPanel.getForm().load({
					method  : 'GET',
			            url     : 'searchleads/showLeadDashBoardDetails?leadDashboardId='+selectedLeadId,
			      		success : function() {
			      					disableLeadForm(); // this function is in lead.js form
			      				 },
			            failure :function() { 
			                		alert ("Failure");
			               		 }
					});
					    formPanel.findById('leadMainTab').setActiveTab('detailsTab');
				};
				

				function reloadLeadSubTabs(tabGrid,leadid)
				{
					tabGrid.store.reload({
	        			params: {
	        						leadId: leadid
	        					}
	        		});
				};
				
		      function saveLead(){
		    	   var formPanel = Ext.getCmp('mainPanel');
		    	   var action = 'Save';
		    	   var selectedLeadId = formPanel.getForm().findField('leadId').getValue();
                   var message = 'No action taken.';
                   
                   if(selectedLeadId == '0'){
                	   action = 'Save';
                	   message = 'Saved Successfully!';
                   }
                   if(selectedLeadId != '0'){
                	   action = 'Update';
                	   message = 'Updated Successfully!';
                   }
                   
                   
         	       var values = formPanel.getForm().getValues();
         	       var encoded = Ext.encode(values);
             	   Ext.Ajax.request({
             		   url		: 'lead/save?action='+action,
             		   method	: 'POST',
             		   success	: function(result, request){
		    			  			var jsonData = Ext.util.JSON.decode(result.responseText); 
		     			  			var responsedLeadId = jsonData.data;
		     			   			alert("Lead "+message);
		     			   			disableLeadForm();
		     			   			formPanel.getForm().findField('leadId').setValue(responsedLeadId);
		     			   			//staticLeadId = responsedLeadId;
		     			   		  },
             		   failure 	: function(){
             			   			alert("failure");
             			   		  },
             		   jsonData : encoded   		  
             	   });
                };
                

                function viewDocument(){
                	 var mConfig = { 
                	       mediaType: 'PDF', 
                	       url: 'form.pdf',
                	       unsupportedText: 'Acrobat Viewer is not Installed',
                	       resizable: true
                	    };                  
                	 var p = new Ext.ux.MediaWindow({  
                	        id: 'PDFViewerWin',
                	        //bodyStyle: 'position:relative; padding:0px;',
                	        renderTo : Ext.getBody(),
                	        width: 600,
                	        height: 400,
                	        mediaCfg: mConfig,
                	        title: 'pdf'
                	  }).show();
                	 
                	};

				function showContactScreen(popup, leadId, contactId ){
						if(popup == true){
							CRM.SPComp.createContactForm(leadId, contactId);
						}
						else{
							//showDetails();
						}
					};
					
				function showLeadActivityScreen(popup, leadId, activityId){
						if(popup == true){
							//CRM.SPComp.createActivityForm(leadId, activityId);
							createActivityForm(leadId, activityId);
						}
						else{
							showDetails();
						}
					};						
					
					
				function disableLeadForm(){
						var form = Ext.getCmp('mainPanel');
						doFormReadOnly(form, leadCompIds, true); //this method is in FormUtil.js
						
						var topButtonPanel 	= Ext.getCmp('leadTopToolBar');
						var buttonPanel 	= Ext.getCmp('leadBottomToolBar');
						
						topButtonPanel.findById('topSaveButton').disable();
						buttonPanel.findById('saveButton').disable();
						
						topButtonPanel.findById('topEditButton').enable();
						buttonPanel.findById('editButton').enable();
						
						form.findById('opportunitiesPanel').enable();
						form.findById('activitiesPanel').enable();
						form.findById('additionalContactPanel').enable();
						form.findById('documentPanel').enable();
						//applyLeadAuthorization(); //this method is in NewHeader.js
				};
				

				
				function editLeadForm(){
						var form = Ext.getCmp('mainPanel');
						doFormReadOnly(form, leadCompIds, false);
						
						var topButtonPanel = Ext.getCmp('leadTopToolBar');
						var buttonPanel = Ext.getCmp('leadBottomToolBar');
						
						buttonPanel.findById('editButton').disable();
						topButtonPanel.findById('topEditButton').disable();
						
						buttonPanel.findById('saveButton').enable();
						topButtonPanel.findById('topSaveButton').enable();
						
						form.findById('opportunitiesPanel').enable();
						form.findById('activitiesPanel').enable();
						form.findById('additionalContactPanel').enable();
						form.findById('documentPanel').enable();	
				};
					
				function newLeadForm(){
						editLeadForm();
						
						var form = Ext.getCmp('mainPanel');
						form.getForm().findField('leadId').setValue(0);
						doFormReadOnly(form, leadCompIds, false);
						form.getForm().reset();
						
						var topButtonPanel = Ext.getCmp('leadTopToolBar');
						var buttonPanel = Ext.getCmp('leadBottomToolBar');
						
						buttonPanel.findById('saveButton').setText('Save');
						topButtonPanel.findById('topSaveButton').setText('Save');
						buttonPanel.findById('saveButton').enable();
						topButtonPanel.findById('topSaveButton').enable();						
						buttonPanel.findById('editButton').disable();
						topButtonPanel.findById('topEditButton').disable();
						
						form.findById('opportunitiesPanel').disable();
						form.findById('activitiesPanel').disable();
						form.findById('additionalContactPanel').disable();
						form.findById('documentPanel').disable();
						
						
						form.findById('leadAnalysisTab').enable();
						
						
						form.findById('opportunitiesGrid').store.removeAll();
						form.findById('activitiesGrid').store.removeAll();
						form.findById('contactsGrid').store.removeAll();
						form.findById('leadDocGrid').store.removeAll();		
						//applyLeadAuthorization(); //this method is in NewHeader.js
				};


				function deactivateLead(leadId, doactivate){
			   			 Ext.MessageBox.show({
			         	            title: 'Confirm',
			         	            msg: 'Are you sure you want to deactivate this Lead.<br>'+
			         	            	 'To activate the Lead again you will have to contact '+
			         	            	 'to authorized person.',
			         	            autoWidth:true,
			         	            buttons: Ext.MessageBox.OKCANCEL,
			         	            multiline: false,
			         	            fn: function(btn, text) {
			         	            	if (btn == 'ok') {
			         	            		activateLead(leadId, doactivate);
			         	            	   }
			         	            	}
			         	           });
			   	};
			   		
			   	function activateLead(leadId, doactivate){
			   			Ext.Ajax.request({
			   				method		: 'POST',
			   				url			: 'lead/activateLead?leadId='+leadId+'&doActivate='+doactivate,
			   				success		: function(){
			   								  Ext.Msg.alert('Message','Lead Deactivated.');
			   								},
			   				failure		: function(){
			   								  Ext.Msg.alert('Failure'); 
			   							    }				
			   			});
			   	};
			   	
			   	function cancelLeadEntryForm(){
			   		var form = Ext.getCmp('mainPanel');
					doFormReadOnly(form, leadCompIds, false);
					form.getForm().reset();
					form.findById('leadMainTab').setActiveTab('searchTab');
					form.findById('detailsTab').disable();
			   	};
				
